#include <my_header.h>
#include "stringStack.h"
//定义函数
int stringStackInit(stringStack_t * pstack)
{
    pstack=(stringStack_t *)calloc(1,sizeof(stringStack_t));
    return 0;
}
int pushStack(stringStack_t * pstack,char * s)
{
    node_t * pNew = (node_t *)calloc(1,sizeof(node_t));
    strncpy(pNew->name,s,strlen(s));
    
    if(pstack->stackSize == 0){     
        pstack->pFront = pNew;
        pstack->pRear = pNew;
    }
    else{
        pstack->pRear->pNext = pNew;
        pstack->pRear = pNew;        
    }
    ++pstack->stackSize;
    return 0;
}
int popStack(stringStack_t * pstack)
{
    node_t * pCur = pstack->pFront;
    if(pstack->stackSize == 1){//栈中只有一个元素
        pstack->pRear = NULL;
        pstack->pFront=NULL;
    }
    else//栈中有多个元素，令pCur指向最后一个元素前一个位置
    {
        int len=pstack->stackSize;
        len-=2;
        while(len)
        {
            pCur=pCur->pNext;
            len--;
        }
        pstack->pRear=pCur;
        pCur=pCur->pNext;
        pstack->pRear->pNext=NULL;
    }
    free(pCur);//清理 
    --pstack->stackSize;
    return 0;

}
int visitStack(stringStack_t * pstack)
{
    node_t * pCur = pstack->pFront;
    while(pCur){
        printf("%s\t", pCur->name);
        pCur = pCur->pNext;

    }
    printf("\n");
    return 0;
}

